<?php
namespace api\modules\v1\controllers\trade;
use api\modules\v1\controllers\CoreController;
use Yii;
use api\modules\models\member\Member;
use api\modules\models\finance\BalanceLog;
use api\modules\models\finance\Balance;
use common\models\platform\BannerModel;
use common\models\finance\StaticInstanceModel;

/*
	账本(积分) 相关控制器
*/

class BalanceController extends CoreController
{



	/*
		获取用户余额
		b_type  账本类型
	*/
	public function actionGet_balance()
	{
		//账本类型
		$btype = $this->request('b_type');
		$data = Balance::getBalance($this->_memberId,$btype);
		$this->out('账本信息',$data);
	}

	/*
		用户账本列表
	*/
	public function actionBalance_list()
	{
		$data = Balance::getAllBalance($this->_memberId); 

		$staticRes = StaticInstanceModel::find()
						->select(['sum(remain_num) as total'])
						->where(['member_id'=>$this->_memberId])->asarray()->one();
		$total = isset($staticRes['total'])?$staticRes['total']:0;
		$static = array('type_name' => '矿池', 'balance' =>$total,'balance_type' => 5,'is_detail' => 0);
		if($this->request('lang')=='en') $static['type_name'] = 'Mining pool';
		$data[] = $static;

		$extend = array();
		$extend['banner'] = BannerModel::bannerList();
		$this->out('账本列表',$data,$extend);
	}


	/*
		积分明细
		* page 当前页
		* page_size 每页数量
		* b_type 账本类型  不传表示所有账本
		* s_type 来源类型  不传表示所有
	*/
	public function actionBalance_log()
	{
		//账本类型
		$btype = $this->request('b_type');
		$stype = $this->request('s_type');

		$whereArr = array();
		$where = ['member_id'=>$this->_memberId];
		if($btype) $where['balance_type'] = $btype;
		if($stype) $where['source_type'] = $stype;

		$whereArr['where'] = $where;
		$params = array(
			'field'	=> ['*'],
			'page'  => $this->request('page',1),
			'limit' => $this->request('page_size',10),
			'order' => 'id desc',
			'lang'  => $this->request('lang','cn'),
		);
		$data = BalanceLog::balanceLogList($whereArr,$params,array('member'));
		$pages = BalanceLog::$pages;
		$this->out('账本明细',$data,array('total_num'=>$pages));
	}


}